package com.aitaoxue.palx.ui.register;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.InjectView;

import com.aitaoxue.palx.AppManager;
import com.aitaoxue.palx.R;
import com.aitaoxue.palx.api.remote.AitaoXueApi;
import com.aitaoxue.palx.base.BaseActivity;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class RegisterActivity2 extends BaseActivity {

	@InjectView(R.id.checkNum)
	EditText checkNum;
	@InjectView(R.id.btn_check)
	Button checkBtn;
	@InjectView(R.id.alert_line)
	RelativeLayout alert;
	@InjectView(R.id.alert_content)
	TextView alertContent;
	@InjectView(R.id.btn_push)
	Button push;

	private CountDownTimer counter;

	private enum CHECK_STATUS {
		ING, END;
	}

	private CHECK_STATUS status = CHECK_STATUS.ING;

	@Override
	protected boolean hasBackButton() {
		return true;
	}

	@Override
	protected int getActionBarTitle() {
		return R.string.register;
	}

	@Override
	protected int getLayoutId() {
		return R.layout.activity_register_2;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_check:
			showToast("验证码已发送,请注意查收", 0, 0);
			trimCounting();
			AitaoXueApi.sendchecknum(getIntent().getStringExtra("phone"),
					mHandler1);
			break;
		case R.id.btn_push:
			String check = checkNum.getText().toString();
			if (TextUtils.isEmpty(check)) {
				showError("请输入验证码");
			} else {
				showWaitDialog();
				AitaoXueApi.checkVerify(check, mHandler2);
			}
			break;
		default:
			break;
		}
	}

	@Override
	public void initView() {
		showToast("验证码已发送,请注意查收", 0, 0);
		trimCounting();

		checkBtn.setOnClickListener(this);
		push.setOnClickListener(this);
		
		AppManager.getAppManager().addActivity(this);
	}

	@Override
	public void initData() {
		// TODO Auto-generated method stub

	}

	private void trimCounting() {
		checkBtn.setEnabled(false);
		counter = new CountDownTimer(60 * 1000, 1000) {

			@Override
			public void onTick(long millisUntilFinished) {
				checkBtn.setText(millisUntilFinished / 1000 + "");
			}

			@Override
			public void onFinish() {
				checkBtn.setEnabled(true);
				checkBtn.setText("获取验证码");
				counter.cancel();
			}
		};

		counter.start();
	}

	@Override
	protected void onDestroy() {
		if (counter != null) {
			counter.cancel();
			counter = null;
		}
		super.onDestroy();
	}

	private AsyncHttpResponseHandler mHandler1 = new AsyncHttpResponseHandler() {

		@Override
		public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
		}

		@Override
		public void onFailure(int arg0, Header[] arg1, byte[] arg2,
				Throwable arg3) {
			// TODO Auto-generated method stub

		}
	};

	private AsyncHttpResponseHandler mHandler2 = new AsyncHttpResponseHandler() {

		@Override
		public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
			hideWaitDialog();
			String res = new String(arg2);

			try {
				JSONObject json = new JSONObject(res);
				String code = json.getString("code");

				if (code.equals("0311")) {
					showError("验证码错误");
				} else if (code.equals("0000")) {
					Intent intent = new Intent(RegisterActivity2.this,
							RegisterActivity3.class);
					intent.putExtra("phone", getIntent()
							.getStringExtra("phone"));
					startActivity(intent);
					//finish();
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		@Override
		public void onFailure(int arg0, Header[] arg1, byte[] arg2,
				Throwable arg3) {
			// TODO Auto-generated method stub

		}
	};

	private void showError(String error) {
		if (alert.getVisibility() == View.GONE) {
			alert.setVisibility(View.VISIBLE);
		}
		alertContent.setText(error);
	}

}
